local guanxing = fk.CreateSkill {

  name = "joyex__guanxing",

  tags = { Skill.Compulsory, },

}



guanxing:addEffect("active", {
  name = "joyex__guanxing",
  anim_type = "control",
  can_use = function(self, player)
    return #player:getPile("joyex__xing") > 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:addPlayerMark(player,"joyex__getxing-turn",#player:getPile("joyex__xing"))
    room:moveCardTo(player:getPile("joyex__xing"), Card.PlayerHand, player, fk.ReasonPrey, guanxing.name)
    if player:getMark("joyex__getxing-turn") >= (#room.alive_players > 2 and 5 or 3) then
      room:setPlayerMark(player,"joyex__zhugelianlu-turn",1)
    end
  end,
})

guanxing:addEffect(fk.CardUsing, {
  refresh_events = {fk.CardUsing},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("joyex__zhugelianlu") > 0 and
      data.card.trueName == "slash" and
      player:usedCardTimes("slash") > 1
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:broadcastPlaySound("./packages/standard_cards/audio/card/crossbow")
    room:setEmotion(player, "./packages/standard_cards/image/anim/crossbow")
    room:sendLog{
      type = "#InvokeSkill",
      from = player.id,
      arg = "crossbow",
    }
  end,
})

guanxing:addEffect("targetmod", {
  name = "#joyex__zhugelianlu",
  frequency = Skill.Compulsory,
  bypass_times = function(self, player, skill, scope)
    if player:getMark("joyex__zhugelianlu-turn") > 0 and skill.trueName == "slash_skill"
      and scope == Player.HistoryPhase then
      return true
    end
  end,
})

guanxing:addEffect(fk.EventPhaseStart, {
  name = "#joyex__guanxing_trigger",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(guanxing.name) then
      return player.phase == Player.Start or player.phase == Player.Finish
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = #room.alive_players > 2 and 5 or 3
    local top = room:getNCards(#room.alive_players > 2 and 5 or 3)
    if #player:getPile("joyex__xing") < (#room.alive_players > 2 and 5 or 3) then
      local ids , _ = U.askforChooseCardsAndChoice(player, top, {"OK"}, guanxing.name, "#joyex__guanxing-card", {"Cancel"}, 1, 1, top)
      if #ids > 0 then
        player:addToPile("joyex__xing",ids, true, guanxing.name)
        top = table.filter(top, function(id) return room:getCardArea(id) ~= Card.PlayerSpecial end)
      else
      end
    end
    room:askForGuanxing(player,top)
  end,
})

return guanxing